var utils = require("../BleMash-js/util.js")
var Ajax = require("../../utils/ajax.js")
var Modal = require("../../utils/modal.js")
var app = getApp()
Page({
  data:{
    index:0,//在all中lights的下标
    name:"暂时的名字",
    lightid:"",
    deviceaddress:"",
    warm:0,
    cold:0,
    colorData: {
      //基础色相，即左侧色盘右上顶点的颜色，由右侧的色相条控制
      hueData: {
        colorStopRed: 255,
        colorStopGreen: 0,
        colorStopBlue: 0,
      },
      //选择点的信息（左侧色盘上的小圆点，即你选择的颜色）
      pickerData: {
        x: 0, //选择点x轴偏移量
        y: 480, //选择点y轴偏移量
        red: 0,
        green: 0,
        blue: 0,
        hex: '#000000'
      },
      //色相控制条的位置
      barY: 0
    },
    rpxRatio: 1, //此值为你的屏幕CSS像素宽度/750，单位rpx实际像素
  },
  onLoad (e) {
    this.setData({
      index:e.index,
      name:e.lightname,
      lightid: e.lightid,//要更名的mesh网络的下标
      deviceaddress: e.deviceaddress,
      rpxRatio: app.globalData.phonewidth / 750
    });
  },
  onChangeColor(e) {
    this.setData({colorData: e.detail.colorData});
    this.addressControl();
  },
  warmchange (e){
    this.setData({warm:e.detail.value})
    this.addressControl();
  },
  coldchange(e) {
    this.setData({cold: e.detail.value});
    this.addressControl();
  },
  addressControl(){
    var self=this;
    var warm = utils.intToHex(self.data.warm);
    var cold = utils.intToHex(self.data.cold);
    var red = utils.intToHex(self.data.colorData.pickerData.red);
    var green = utils.intToHex(self.data.colorData.pickerData.green);
    var blue = utils.intToHex(self.data.colorData.pickerData.blue);
    app.globalData.activemesh.addressControl(self.data.deviceaddress, cold, warm, red, green, blue, function (value) {
      if(value.code==0){
        var data = {
          lightid: self.data.lightid,
          name: self.data.name,
          cold: self.data.cold,
          warm: self.data.warm,
          red: self.data.colorData.pickerData.red,
          green: self.data.colorData.pickerData.green,
          blue: self.data.colorData.pickerData.blue,
        };
        Ajax.AsyncPost("/light/edit_light/", data);
      }
    });
  }
})
